package com.koron.css2.systemManage.servlet;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.dto.RoleOperationDto;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 添加角色操作
 * @author 59532
 *
 */
public class AddRoleOperation implements ServerInterface{

	private static Logger log = LogManager.getLogger(AddRoleOperation.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", Void.class);
		try {
			RoleOperationDto roleOperation = JsonUtils.objectToPojo(req.getData(), RoleOperationDto.class);
			if(StringUtils.isEmpty(roleOperation.getRoleCode())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("角色不能为空");
				return msg;
			}
			if(roleOperation.getOperation() == null || roleOperation.getOperation().size() == 0) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("菜单不能为空");
				return msg;
			}
			ServiceFactory.getOperationService().addRoleOperation(roleOperation.getRoleCode(), roleOperation.getOperation());
		} catch (Exception e) {
			log.error("添加角色菜单失败：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("添加角色菜单失败");
		}
		return msg;
	}

}
